﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using static DevExpress.XtraReports.Design.View.Office2003PaintHelper;
using System.Collections;
using DiaoPaiDaYin;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Editors;
using DevExpress.Utils.Win;
using DevExpress.XtraLayout;
using DevExpress.XtraGrid.Demos.util;
using System.Diagnostics;
using DXApplicationTangche.UC.门店下单.下单;
using Luna小店.UC.门店下单.DTO;
using DXApplicationTangche.UC.门店下单.service;

namespace DXApplicationTangche.UC.门店下单.下单
{
    public partial class XtraUserControlC设计点 : DevExpress.XtraEditors.XtraUserControl
    {
        #region 属性
        private 设计点Model model设计点;
        public List<DTODesignPoints> Dto设计点s = new List<DTODesignPoints>();
        private GridEditorCollection gridEditors;
        #endregion
        
        public XtraUserControlC设计点(String styleNameCn)
        {
            InitializeComponent();
            //  查询默认及可选设计点并绑定控件及数据源
            this.model设计点 = new 设计点Model(styleNameCn);
            this.Dto设计点s = this.model设计点.build原默认设计点().defaultDtoDesignPoints;
            this.gridEditors = new GridEditorCollection(this.model设计点.designPointsFacadeDTOs);
            this.gridControl设计点.DataSource = this.gridEditors;
        }
        
        private void gridView1_CustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
        {
            if (e.Column == this.gridEditorValue)
            {
                GridEditorItem item = gridView设计点.GetRow(e.RowHandle) as GridEditorItem;
                if (item != null) e.RepositoryItem = item.RepositoryItem;
            }
        }
        private void gridView设计点_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            try
            {
                if (e.Column.Name == "gridEditorName" || e.Column.Name == "gridEditorValue")
                {
                    if (this.Dto设计点s[e.RowHandle].optionValue != this.Dto设计点s[e.RowHandle].defaultValue)
                    {
                        e.Appearance.BackColor = Color.Red;
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        public class GridEditorItem
        {
            string fName;
            object fValue;
            RepositoryItem fRepositoryItem;

            public GridEditorItem(RepositoryItem fRepositoryItem, string fName, object fValue)
            {
                this.fRepositoryItem = fRepositoryItem;
                this.fName = fName;
                this.fValue = fValue;
            }

            public GridEditorItem(DesignPointsFacadeDTO designPointsFacadeDTO) {
                this.fRepositoryItem = new RepositoryItemSearchLookUpEdit可选设计点(designPointsFacadeDTO);
                this.fName = designPointsFacadeDTO.defaultDtoDesignPoints.itemTypeNameCn;
                this.fValue = designPointsFacadeDTO.optionDtoDesignPoints;
            }
            public string Name { get { return this.fName; } }
            public object Value { get { return this.fValue; } set { this.fValue = value; } }
            public RepositoryItem RepositoryItem { get { return this.fRepositoryItem; } }
        }

        class GridEditorCollection : ArrayList
        {
            public GridEditorCollection()
            {
            }
            public new GridEditorItem this[int index] { get { return base[index] as GridEditorItem; } }
            public void Add(RepositoryItem fRepositoryItem, string fName, object fValue)
            {
                base.Add(new GridEditorItem(fRepositoryItem, fName, fValue));
            }
            public GridEditorCollection(List<DesignPointsFacadeDTO> designPointsFacadeDTOs)
            {
                foreach (DesignPointsFacadeDTO item in designPointsFacadeDTOs)
                {
                    this.Add(new GridEditorItem(item));
                }
            }
        }
    }
    public class 设计点Model
    {
        public List<DesignPointsFacadeDTO> designPointsFacadeDTOs = new List<DesignPointsFacadeDTO>();
        public List<DTODesignPoints> defaultDtoDesignPoints = new List<DTODesignPoints>();
        public 设计点Model(String styleNameCn)
        {
            //查询设计点
            this.designPointsFacadeDTOs = 下单相关Service.getDesignPointsFacadeByStyleName(styleNameCn);
        }
        public 设计点Model build原默认设计点()
        {
            foreach (DesignPointsFacadeDTO item in this.designPointsFacadeDTOs)
            {
                this.defaultDtoDesignPoints.Add(item.defaultDtoDesignPoints);
            }
            return this;
        }
    }
}
